import { HOME_ACTION_TYPES } from "../actions/home"
import { RecordMode } from "../common/enums"

let forceReloadIndex = 0
const defaultActivePage = ''
const rawState = {
    activeTab: 'progress',
    common: {
        forceReloadAllPlansIndex: 0,
        allPlans: [],
        allPages: [],
        allEnvs: [],
        allCookies: []
    },
    progress: {
        collapseCol0: false,
        collapseCol1: false,
        forceReloadActivePlanLogsIndex: 0,
        activePlan: {
            id: '',
            title: '',
            env: '',
            baseEnv: '',
        },
        activePlanSuites: [],
        activePlanLogStat: [],
        activeSuiteLogs: [],
        activeSuiteStepList: [],
        activeSuite: {
            id: '',
            title: '',
            level: '',
            stepList: []
        }
    },
    runner: {
        collapseCol0: false,
        collapseCol1: false,
        activePage: defaultActivePage,
        acitveSuiteId: '',
    },
    plan: {
        collapseCol0: false,
        collapseCol1: false,
        mode: '',
        activePlanId: '',
        title: '',
        captureScreenShot: 'Y',
        retryTimesOnError: '0',
        env: {
            name: '',
            host: ''
        },
        baseEnv: {
            name: '',
            host: ''
        },
        suites: [],
        addedPlans: [],  // 用于存放暂时新增没有保存的plan
        suiteIds: []
    },
    suite: {
        collapseCol0: false,
        collapseCol1: false,
        activePage: defaultActivePage,
        acitveSuiteId: '',
        title: '',
        level: '',
        mode: '',   //当前是在新增/修改suit模式
        forceReloadIndex: 0
    },
    stepCollection: {
        collapseCol0: false,
        collapseCol1: false,
        acitveStepCollectionId: '',
        title: '',
        mode: '',   //当前是在新增/修改step collection模式
        forceReloadIndex: 0
    },
    element: {
        collapseCol0: false,
        collapseCol1: false,
        activePage: defaultActivePage,
        activeNode: null
    },
    network: {
        collapseCol0: false,
        collapseCol1: false,
        activePage: defaultActivePage,
    },
    mock: {
        collapseCol0: false,
        collapseCol1: false,
        activePage: defaultActivePage,
        acitveAjaxId: 0
    },
    config: {
        acitveConfigType: 'env'
    },
    notify: {
        type: '',
        message: ''
    },
    log: {
        stat: {}        // { suiteId1: {total: 10, finish: 5, page: 'home'}, suiteId2: {total: 20, finish: 15, page: 'search'} }
    },
}

export const reducer = (state = rawState, action) => {
    switch (action.type) {
        // ROOT
        case HOME_ACTION_TYPES.ROOT_SET_NOTIFY: {
            return {
                ...state,
                notify: {
                    type: action.notifyType,
                    message: action.message
                }
            }
        }
        case HOME_ACTION_TYPES.ROOT_SET_ACTIVE_TAB: {
            return {
                ...state,
                activeTab: action.tab
            }
        }
        case HOME_ACTION_TYPES.ROOT_SET_ACTIVE_PAGE_OF_ACTIVE_TAB: {
            return {
                ...state,
                [state.activeTab]: {
                    ...state[state.activeTab],
                    activePage: action.page.name
                }
            }
        }
        case HOME_ACTION_TYPES.ROOT_SET_ACTIVE_TAB_COLLAPSE_COL: {
            return {
                ...state,
                [state.activeTab]: {
                    ...state[state.activeTab],
                    [`collapseCol${action.colIndex}`]: action.collapse
                }
            }
        }
        // PROGRESS
        case HOME_ACTION_TYPES.PROGRESS_SET_ACTIVE_PLAN: {
            return {
                ...state,
                progress: {
                    ...state.progress,
                    activePlan: action.plan,
                    // activeSuite: action.plan.suites[0] || state.progress.activeSuite
                },
            }
        }
        case HOME_ACTION_TYPES.PROGRESS_SET_ACTIVE_PLAN_SUITES: {
            return {
                ...state,
                progress: {
                    ...state.progress,
                    activePlan: {
                        ...state.progress.activePlan,
                    },
                    activePlanSuites: action.suites,
                    activeSuite: action.suites.length ? action.suites[0] : state.progress.activeSuite
                },
            }
        }
        case HOME_ACTION_TYPES.PROGRESS_FORCE_RELOAD_ACTIVE_PLAN_LOGS: {
            return {
                ...state,
                progress: {
                    ...state.progress,
                    forceReloadActivePlanLogsIndex: ++forceReloadIndex
                },
            }
        }
        case HOME_ACTION_TYPES.PROGRESS_SET_ACTIVE_PLAN_LOGS: {
            return {
                ...state,
                progress: {
                    ...state.progress,
                    activePlanLogStat: action.logStat
                },
            }
        }
        case HOME_ACTION_TYPES.PROGRESS_SET_ACTIVE_SUITE: {
            return {
                ...state,
                progress: {
                    ...state.progress,
                    activeSuite: action.suite
                }
            }
        }
        case HOME_ACTION_TYPES.PROGRESS_SET_ACTIVE_SUITE_LOGS: {
            return {
                ...state,
                progress: {
                    ...state.progress,
                    activeSuiteLogs: action.logs,
                }
            }
        }
        case HOME_ACTION_TYPES.PROGRESS_SET_ACTIVE_SUITE_STEP_LIST: {
            return {
                ...state,
                progress: {
                    ...state.progress,
                    activeSuiteStepList: action.stepList,
                }
            }
        }
        // COMMON
        case HOME_ACTION_TYPES.COMMON_SET_ALL_PLANS: {
            return {
                ...state,
                common: {
                    ...state.common,
                    allPlans: action.plans,
                }
            }
        }
        case HOME_ACTION_TYPES.COMMON_SET_ALL_PAGES: {
            return {
                ...state,
                common: {
                    ...state.common,
                    allPages: action.pages,
                }
            }
        }
        case HOME_ACTION_TYPES.COMMON_SET_ALL_ENVS: {
            return {
                ...state,
                common: {
                    ...state.common,
                    allEnvs: action.envs,
                }
            }
        }
        case HOME_ACTION_TYPES.COMMON_SET_ALL_COOKIES: {
            return {
                ...state,
                common: {
                    ...state.common,
                    allCookies: action.cookies,
                }
            }
        }
        case HOME_ACTION_TYPES.COMMON_FORCE_RELOAD_ALL_PLANS: {
            return {
                ...state,
                common: {
                    ...state.common,
                    forceReloadAllPlansIndex: ++forceReloadIndex,
                }
            }
        }
        // RUNNER
        case HOME_ACTION_TYPES.RUNNER_SET_ACTIVE_SUITE: {
            return {
                ...state,
                runner: {
                    ...state.runner,
                    acitveSuiteId: action.suite.id
                }
            }
        }
        // SUITE
        case HOME_ACTION_TYPES.SUITE_SET_ACTIVE_SUITE: {
            return {
                ...state,
                suite: {
                    ...state.suite,
                    acitveSuiteId: action.suite.id,
                    title: action.suite.title,
                    level: action.suite.level,
                    mode: action.suite.mode || RecordMode.edit
                }
            }
        }
        case HOME_ACTION_TYPES.SUITE_FORCE_RELOAD_SUIT_LIST: {
            return {
                ...state,
                suite: {
                    ...state.suite,
                    ...rawState.suite,
                    collapseCol0: state.suite.collapseCol0,
                    collapseCol1: state.suite.collapseCol1,
                    activePage: state.suite.activePage,
                    forceReloadIndex: ++forceReloadIndex,
                }
            }
        }
        // STEP COLLECTION
        case HOME_ACTION_TYPES.STEP_COLLECTION_SET_ACTIVE_STEP_COLLECTION: {
            return {
                ...state,
                stepCollection: {
                    ...state.stepCollection,
                    acitveStepCollectionId: action.stepCollection.id,
                    title: action.stepCollection.title,
                    mode: action.stepCollection.mode || RecordMode.edit
                }
            }
        }
        case HOME_ACTION_TYPES.STEP_COLLECTION_RELOAD_LIST: {
            return {
                ...state,
                stepCollection: {
                    ...state.stepCollection,
                    ...rawState.stepCollection,
                    forceReloadIndex: ++forceReloadIndex,
                }
            }
        }
        // ELEMENT
        case HOME_ACTION_TYPES.ELEMENT_SET_ACTIVE_NODE: {
            return {
                ...state,
                element: {
                    ...state.element,
                    activeNode: action.node
                }
            }
        }
        // AJAX
        // MOCK
        case HOME_ACTION_TYPES.MOCK_SET_ACTIVE_AJAX_ID: {
            return {
                ...state,
                mock: {
                    ...state.mock,
                    acitveAjaxId: action.ajaxId
                }
            }
        }
        // CONFIG
        case HOME_ACTION_TYPES.ROOT_SET_ACTIVE_CONFIG_TYPE: {
            return {
                ...state,
                config: {
                    ...state.config,
                    acitveConfigType: action.configType
                }
            }
        }
        // RUNNER
        case HOME_ACTION_TYPES.LOG_SET_STAT: {
            return {
                ...state,
                log: {
                    ...state.log,
                    stat: action.stat
                }
            }
        }
        // PLAN
        case HOME_ACTION_TYPES.PLAN_ADD_PLAN: {
            return {
                ...state,
                plan: {
                    ...state.plan,
                    addedPlans: [action.plan, ...state.plan.addedPlans],
                }
            }
        }
        case HOME_ACTION_TYPES.PLAN_CLEAR_ADDED_PLANS: {
            return {
                ...state,
                plan: {
                    ...state.plan,
                    addedPlans: [],
                }
            }
        }
        case HOME_ACTION_TYPES.PLAN_SET_ACTIVE_PLAN: {
            return {
                ...state,
                plan: {
                    ...state.plan,
                    ...{
                        ...action.plan,
                        id: undefined,
                        mode: action.plan.mode || ''
                    },
                    activePlanId: action.plan.id,
                }
            }
        }
        case HOME_ACTION_TYPES.PLAN_SET_ACTIVE_PLAN_SUITES: {
            return {
                ...state,
                plan: {
                    ...state.plan,
                    ...{
                        ...action.plan,
                        id: undefined,
                        mode: action.plan.mode || ''
                    },
                    activePlanId: action.plan.id,
                }
            }
        }
        case HOME_ACTION_TYPES.PLAN_CHANGE_ACTIVE_PLAN_FIELD: {
            return {
                ...state,
                plan: {
                    ...state.plan,
                    [action.field]: action.value
                }
            }
        }
        default:
            return state
    }
}
